// Copyright (C) 2008 Taylor L. Riche <riche@cs.utexas.edu>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#include "Application.h"

namespace lagniappe
{

Application::Application()
{
}

Application::~Application()
{
}

void
Application::setup(System * sys, void * arg_struct)
{
	this->buildOperators(sys, arg_struct);
	this->connectGraph();
	sys->createResources();
	activateMonitor(sys);
	this->schedule(sys);
	return;
}

void
Application::setupProfiler(System * sys)
{
	this->buildOperators(sys, NULL, true);
	return;
}

void
Application::startLoop(System * sys)
{
	reader::Reader appReader;
	while(true) {
//		std::string lineRead;
//		lineRead = appReader.getLine();
//
//		if(lineRead.find(commands::CMD_QUIT,0) == 0) {
//			break;
//		}
//		if(lineRead.find(commands::CMD_REPORT,0) == 0) {
//			sys->reportChannelUsage();
//		}
//		if(lineRead.find(commands::CMD_LIST,0) == 0) {
//			opMap["eth0"]->list();
//			opMap["eth1"]->list();
//		}
	}

}

void
Application::activateMonitor(System * sys)
{
	Monitor * mon = new Monitor(sys);
	lagniappe::ProcElement * tempProc = sys->getFreeProc(mon, lagniappe::INITIAL);
    //if(tempProc == NULL) tempProc = sys->roundRobin();
    sys->assignProc(tempProc, mon);
    assert(tempProc != NULL);
	mon->schedule(tempProc);
	mon->registerPorts(tempProc->getSrcChan());
	for(std::vector<CoreOp *>::iterator iter = opVec.begin(); iter != opVec.end(); iter++) {
		Operator * oper = dynamic_cast<Operator *>(*iter);
		if(oper != NULL) {
			assert(mon != NULL);
			// TLR: OPT
			oper->setDestination(MONITOR_OUT_PORT, new std::pair<CoreOp *, uint32_t>(mon, 0));
		}

	}
}

}
